package fat
{

import com.fit4as.fit.*;
import mx.utils.StringUtil;
import com.fit4as.util.io.StringWriter;

public class AnnotationFixture extends ColumnFixture
{
    public var OriginalHTML : String;
    public var Row : int;
    public var Column : int;

    public var OverwriteCellBody : String;
    public var AddToCellBody : String;

    public var OverwriteCellTag : String;
    public var OverwriteEndCellTag : String;
    public var AddToCellTag : String;

    public var OverwriteRowTag : String;
    public var OverwriteEndRowTag : String;
    public var AddToRowTag : String;

    public var OverwriteTableTag : String;
    public var OverwriteEndTableTag : String;
    public var AddToTableTag : String;

    public var AddCellFollowing : String;
    public var RemoveFollowingCell : String;

    public var AddRowFollowing : String;
    public var RemoveFollowingRow : String;

    public var AddTableFollowing : String;

    public function ResultingHTML() : String {
        var table : Parse = Parse.createParse(OriginalHTML);
        var row : Parse = table.at(0, Row - 1);
        var cell : Parse = row.at(0, Column - 1);

        if (OverwriteCellBody != null) cell.body = OverwriteCellBody;
        if (AddToCellBody != null) cell.addToBody(AddToCellBody);

        if (OverwriteCellTag != null) cell.tag = OverwriteCellTag;
        if (OverwriteEndCellTag != null) cell.end = OverwriteEndCellTag;
        if (AddToCellTag != null) cell.addToTag(stripDelimiters(AddToCellTag));

        if (OverwriteRowTag != null) row.tag = OverwriteRowTag;
        if (OverwriteEndRowTag != null) row.end = OverwriteEndRowTag;
        if (AddToRowTag != null) row.addToTag(stripDelimiters(AddToRowTag));

        if (OverwriteTableTag != null) table.tag = OverwriteTableTag;
        if (OverwriteEndTableTag != null) table.end = OverwriteEndTableTag;
        if (AddToTableTag != null) table.addToTag(stripDelimiters(AddToTableTag));

        if (AddCellFollowing != null) addParse(cell, AddCellFollowing, ["td"]);
        if (RemoveFollowingCell != null) removeParse(cell);

        if (AddRowFollowing != null) addParse(row, AddRowFollowing, ["tr", "td"]);
        if (RemoveFollowingRow != null) removeParse(row);

        if (AddTableFollowing != null) addParse(table, AddTableFollowing, ["table", "tr", "td"]);

        return GenerateOutput(table);
    }

    private function addParse(parse : Parse, newString : String, tags : Array) : void {
        var newParse : Parse = Parse.createParse(newString, tags);
        newParse.more = parse.more;
        newParse.trailer = parse.trailer;
        parse.more = newParse;
        parse.trailer = null;
    }

    private function removeParse(parse : Parse) : void {
        parse.trailer = parse.more.trailer;
        parse.more = parse.more.more;
    }

    private function stripDelimiters(s : String) : String {
        return s.replace(/^\[|\]$/g, "");
    }

    // code smell note: copied from DocumentParseFixture
    private function GenerateOutput(document : Parse) : String {
        var result : StringWriter = new StringWriter();
        document.print(result);
        return StringUtil.trim(result.getContent());
    }
}
}